#include "ActionTranspond.h"
#include "ActionBase.h"
#include "context.h"
#include "util.h"
#include "streamdata.h"
#include "Md5.h"
#include "json.h"
#include "3300_gm.pb.h"


static bool __init__ = ActionTranspond::Init();

bool ActionTranspond::Init()
{
    Ctx.msg_router.BindHttpHandle("post", "/transpond", ActionTranspond::HandleTranspond);

    return true;
}

void ActionTranspond::HandleTranspond(HttpData&& data)
{
    int err = 0;
    std::string body = "";
    do
    {
        Json::Value http_req_json;
        if (!CTools::FromStr(http_req_json, data.body))
        {
            log_error("json parser failed, url:%s, body:%s", data.target.c_str(), data.body.c_str());
            err = 1;
            break;
        }

		std::int32_t sv_type = http_req_json["server_type"].asInt();
		std::int32_t svid =  http_req_json["server_id"].asInt();
		std::int32_t cmd =  http_req_json["cmd"].asInt();
		std::string data_base64 =  http_req_json["data"].asString();
		
		log_info("sv_type:%d,svid:%d,data:%s", sv_type, svid, data_base64.c_str());
		if (sv_type == 0) 
		{
			body = "server_type not exist";
			break;
		}
		if (cmd == 0)
		{
			body = "cmd is 0";
			break;
		}

		std::string data = CTools::Base64(data_base64, false);

		if (svid == 0)
		{
            Ctx.host->MtBroadcastMessageDirect(cmd, std::move(data), sv_type);
		}
		else
		{
			Ctx.host->MtSendtoOneSrv(cmd, std::move(data), sv_type, svid);
		}
        body = "ok";
    } while (false);
    ResponseHttp(data.request_id, err, body);
}













